﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TomShane.Neoforce.Controls;
using System.Xml;
using System.Xml.Serialization;

namespace GameLib
{
    [XmlRoot("Server")]
    public class Server
    {
        [XmlAttribute("Type")]
        public string Type { get; set; }
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("Port")]
        public int Port { get; set; }
        [XmlAttribute("IP")]
        public string IP { get; set; }
    }

    [XmlRoot("ServerList")]
    public class ServerList : List<Server>
    {
    }

    public class ServerSelectionControl : Component
    {
        Window serverSelectionWindow;
        Label serverLabel;
        ListBox serverList;

        string sServerListFile = "Content\\Config\\ServerList.xml"; 
        XmlSerializer xmlSerializer;

        public override void Init()
        {
            base.Init();
            serverSelectionWindow = new Window(Manager);
            serverSelectionWindow = new Window(Manager);
            serverSelectionWindow.Init();
            serverSelectionWindow.Text = "Test";
            serverSelectionWindow.Width = 200;
            serverSelectionWindow.Height = Manager.Graphics.PreferredBackBufferHeight - 200;
            serverSelectionWindow.Movable = false;
            serverSelectionWindow.Resizable = false;
            serverSelectionWindow.BorderVisible = false;
            serverSelectionWindow.Left = 10;
            serverSelectionWindow.Top = 100;// Manager.Graphics.PreferredBackBufferHeight / 2 + serverSelectionWindow.ClientHeight;
            serverSelectionWindow.Visible = false;

            serverLabel = new Label(Manager);
            serverLabel.Width = serverSelectionWindow.Width;
            serverLabel.Height = 25;
            serverLabel.Alignment = Alignment.MiddleCenter;
            serverLabel.Text = "Server List";
            serverLabel.Parent = serverSelectionWindow;

            serverList = new ListBox(Manager);
            serverList.Top = 30;
            serverList.Left = 10;
            serverList.Width = serverSelectionWindow.Width - 20;
            serverList.Height = serverSelectionWindow.Height - 45;
            xmlSerializer = new XmlSerializer(typeof(ServerList));
            var oServers = xmlSerializer.Deserialize(XmlReader.Create(sServerListFile));
            ServerList servers = oServers as ServerList;
            foreach (Server s in servers)
            {
                serverList.Items.Add(s.Name + " - " + s.IP + ":" + s.Port);
            }
                //serverList.Items.Add("Server 1");
            //serverList.Items.Add("Server 2");
            //serverList.Items.Add("Server 3");
            //serverList.Items.Add("Server 4");
            //serverList.Items.Add("Server 5");
            serverList.Parent = serverSelectionWindow;

            Manager.Add(serverSelectionWindow);
        }

        public void Show()
        {
            serverSelectionWindow.Visible = true;
            serverSelectionWindow.ShowModal();
        }


        public ServerSelectionControl(Manager manager) 
            : base(manager) 
        {}

    }
}
